import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
	state: {
		network: true,
		token: uni.getStorageSync('token') || null,
		user_info: uni.getStorageSync('user_info') || {},
		systemInfo: {
			statusBarHeight: 0
		},
		loading: false, //加载框是否正在显示
		option: {},
		lang: 'zh',
		is_app: false,
		dev: "H5",
		msg_count: 0,
	},
	mutations: {
		setMsgCount(state, provider) {
			state.msg_count = provider
		},
		setDev(state, provider) {
			state.dev = provider
		},
		setIsApp(state, provider) {
			state.is_app = provider
		},
		setLang(state, provider) {
			if (provider === 'zh') {
				uni.setStorageSync('mescroll-i18n', 'zh')
			} else {
				uni.setStorageSync('mescroll-i18n', 'en')
			}

			uni.setStorageSync('lang', provider);
			state.lang = provider;
			window.location.reload()
		},
		setNetwork(state, provider) {
			state.network = provider;
		},
		setOption(state, provider) {
			state.option = provider;
		},
		loadingShow(state, provider) {
			if (state.loading === false) {
				uni.showLoading({
					title: provider
				})
				state.loading = true
			}
		},
		loadingHide(state) {
			setTimeout(function() {
				uni.hideLoading();
				state.loading = false
			}, 500);
		},
		// 登录
		login(state, provider) {
			uni.setStorageSync('token', provider);
			state.token = provider;
		},
		//登出
		loginout(state) {
			if (Vue.prototype.socket) Vue.prototype.socket.close()
			uni.removeStorageSync('token') //清除本地token缓存
			uni.removeStorageSync('user_info') //清除本地token缓存
			state.user_info = {};
			state.token = null;
		},
		setUserInfo(state, provider) {
			const user = { ...state.user_info, ...provider }
			uni.setStorageSync('user_info', user);
			state.user_info = user;
		},


		//设置设备型号 ios 安卓
		setSystemInfo(state, provider) {
			state.systemInfo = provider;
		},

	},
})
export default store